private int readAvailable(byte[] bytes) throws IOException,InterruptedException{ int i = 0, a; boolean foundPrefix = false, foundSuffix = false; int ch = inputStream.read(); if(ch < 0)return ch; if (isPreSuffixEnabled) {//找前缀 if (ch != PREFIX) { while (inputStream.available() > 0) { if (inputStream.read() == PREFIX) { foundPrefix = true; break; } }
时间: 2024-02-10 20:19:44 浏览: 39
这段代码是接收线程中的一个方法,用于读取蓝牙设备发送的数据,并将数据存储到字节数组中。首先定义了一个变量i用于记录字节数组中已经存储的数据长度,以及一个变量a用于记录当前读取到的字节。接着定义了两个标志变量foundPrefix和foundSuffix,用于判断前缀和后缀是否被找到。然后读取输入流中的一个字节,并判断读取的字节是否小于0,如果小于0则表示输入流已经被关闭,返回读取到的字节数。如果启用了前后缀模式,则判断当前读取到的字节是否等于前缀,如果不等于,则循环读取输入流中的数据,直到找到前缀为止。如果找到了前缀,则将foundPrefix标志设置为true。这个方法可以在Android中用于读取蓝牙设备发送的数据,并处理前后缀模式。
相关问题
public synchronized void write(K key, V value) throws IOException { boolean nullKey = key == null || key instanceof NullWritable; boolean nullValue = value == null || value instanceof NullWritable; if (nullKey && nullValue) { return; } if (!nullKey) { writeObject(key); } if (!(nullKey || nullValue)) { out.write(keyValueSeparator); } if (!nullValue) { writeObject(value); } out.write(newline); } public synchronized void close(TaskAttemptContext context) throws IOException { out.close(); } } public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job ) throws IOException, InterruptedException { Configuration conf = job.getConfiguration(); boolean isCompressed = getCompressOutput(job); String keyValueSeparator= conf.get(SEPERATOR, "\t"); CompressionCodec codec = null; String extension = ""; if (isCompressed) { Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class); codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf); extension = codec.getDefaultExtension(); } Path file = getDefaultWorkFile(job, extension); FileSystem fs = file.getFileSystem(conf); if (!isCompressed) { FSDataOutputStream fileOut = fs.create(file, false); return new LineRecordWriter<K, V>(fileOut, keyValueSeparator); } else { FSDataOutputStream fileOut = fs.create(file, false); return new LineRecordWriter<K, V>(new DataOutputStream (codec.createOutputStream(fileOut)), keyValueSeparator); } } }请解释以上代码
这段代码是一个 Hadoop 的 MapReduce 程序中的输出部分的代码。主要包括了 `LineRecordWriter` 这个类的实现和 `getRecordWriter()` 方法的实现。
`LineRecordWriter` 实现了 `RecordWriter` 接口,用于将 MapReduce 程序的输出按照行的形式写入到文件中。`write()` 方法用于将键值对写入文件,如果键或值为 null 则不写入。`close()` 方法用于关闭输出流。
`getRecordWriter()` 方法用于获取一个 `RecordWriter` 对象。该方法首先获取配置信息,判断是否需要压缩输出。如果需要压缩输出,则获取压缩编解码器的类并创建一个对应的压缩文件;否则创建一个普通文件。最后返回一个 `LineRecordWriter` 对象,用于写入数据。
总体来说,这段代码实现了 MapReduce 程序的输出功能,支持压缩和非压缩两种格式的输出。
public class TCAPP { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { // 1.获取job Configuration conf=new Configuration(); Job job=Job.getInstance(conf); // 2.指定job使用的类
job.setJarByClass(TCAPP.class);
// 3.指定map和reduce使用的类
job.setMapperClass(TCMapper.class);
job.setReducerClass(TCReducer.class);
// 4.指定map输出的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 5.指定reduce输出的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 6.指定输入和输出路径
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
// 7.提交job
boolean result=job.waitForCompletion(true);
System.exit(result?0:1);
}
这是一个MapReduce程序的主函数,可以实现对文本数据进行词频统计。其中,指定了Map和Reduce类,指定了输入和输出路径,最后提交job并退出程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)