Search in sources :

Example 61 with OutputStream

use of java.io.OutputStream in project bazel by bazelbuild.

the class SkylarkRepositoryIntegrationTest method testCycleErrorInWorkspaceFileWithExternalRepo.

@Test
public void testCycleErrorInWorkspaceFileWithExternalRepo() throws Exception {
    try (OutputStream output = scratch.resolve("WORKSPACE").getOutputStream(true)) {
        output.write(("\nload('//foo:bar.bzl', 'foobar')" + "\ngit_repository(name = 'git_repo')").getBytes(StandardCharsets.UTF_8));
    }
    scratch.file("BUILD", "");
    scratch.file("foo/BUILD", "");
    scratch.file("foo/bar.bzl", "load('@git_repo//xyz:foo.bzl', 'rule_from_git')", "rule_from_git(name = 'foobar')");
    invalidatePackages();
    try {
        getTarget("@//:git_repo");
        fail();
    } catch (AssertionError expected) {
        assertThat(expected.getMessage()).contains("Failed to load Skylark extension " + "'@git_repo//xyz:foo.bzl'.\n" + "It usually happens when the repository is not defined prior to being used.\n" + "Maybe repository 'git_repo' was defined later in your WORKSPACE file?");
    }
}
Also used : OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 62 with OutputStream

use of java.io.OutputStream in project cas by apereo.

the class OneTimeTokenQRGeneratorController method generateQRCode.

private static void generateQRCode(final OutputStream stream, final String key) {
    try {
        final Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
        hintMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
        hintMap.put(EncodeHintType.MARGIN, 2);
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        final QRCodeWriter qrCodeWriter = new QRCodeWriter();
        final BitMatrix byteMatrix = qrCodeWriter.encode(key, BarcodeFormat.QR_CODE, 250, 250, hintMap);
        final int width = byteMatrix.getWidth();
        final BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        final Graphics2D graphics = (Graphics2D) image.getGraphics();
        try {
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, width, width);
            graphics.setColor(Color.BLACK);
            IntStream.range(0, width).forEach(i -> {
                IntStream.range(0, width).filter(j -> byteMatrix.get(i, j)).forEach(j -> graphics.fillRect(i, j, 1, 1));
            });
        } finally {
            graphics.dispose();
        }
        ImageIO.write(image, "png", stream);
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Color(java.awt.Color) OutputStream(java.io.OutputStream) IntStream(java.util.stream.IntStream) BufferedImage(java.awt.image.BufferedImage) EnumMap(java.util.EnumMap) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) Throwables(com.google.common.base.Throwables) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) HttpServletRequest(javax.servlet.http.HttpServletRequest) Graphics2D(java.awt.Graphics2D) EncodeHintType(com.google.zxing.EncodeHintType) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) GetMapping(org.springframework.web.bind.annotation.GetMapping) BitMatrix(com.google.zxing.common.BitMatrix) BarcodeFormat(com.google.zxing.BarcodeFormat) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) BitMatrix(com.google.zxing.common.BitMatrix) EnumMap(java.util.EnumMap) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 63 with OutputStream

use of java.io.OutputStream in project glide by bumptech.

the class StreamEncoder method encode.

@Override
public boolean encode(InputStream data, File file, Options options) {
    byte[] buffer = byteArrayPool.get(ArrayPool.STANDARD_BUFFER_SIZE_BYTES, byte[].class);
    boolean success = false;
    OutputStream os = null;
    try {
        os = new FileOutputStream(file);
        int read;
        while ((read = data.read(buffer)) != -1) {
            os.write(buffer, 0, read);
        }
        os.close();
        success = true;
    } catch (IOException e) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Failed to encode data onto the OutputStream", e);
        }
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            // Do nothing.
            }
        }
        byteArrayPool.put(buffer, byte[].class);
    }
    return success;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 64 with OutputStream

use of java.io.OutputStream in project CoreNLP by stanfordnlp.

the class SimpleSentiment method main.

public static void main(String[] args) throws IOException {
    RedwoodConfiguration.standard().apply();
    startTrack("main");
    // Read the data
    Stream<SentimentDatum> data = Stream.concat(Stream.concat(Stream.concat(imdb("/users/gabor/tmp/aclImdb/train/pos", SentimentClass.POSITIVE), imdb("/users/gabor/tmp/aclImdb/train/neg", SentimentClass.NEGATIVE)), Stream.concat(imdb("/users/gabor/tmp/aclImdb/test/pos", SentimentClass.POSITIVE), imdb("/users/gabor/tmp/aclImdb/test/neg", SentimentClass.NEGATIVE))), Stream.concat(Stream.concat(stanford("/users/gabor/tmp/train.tsv"), stanford("/users/gabor/tmp/test.tsv")), Stream.concat(twitter("/users/gabor/tmp/twitter.csv"), unlabelled("/users/gabor/tmp/wikipedia"))));
    // Train the model
    OutputStream stream = IOUtils.getFileOutputStream("/users/gabor/tmp/model.ser.gz");
    SimpleSentiment classifier = SimpleSentiment.train(data, Optional.of(stream));
    stream.close();
    log.info(classifier.classify("I think life is great"));
    endTrack("main");
// 85.8
}
Also used : ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream)

Example 65 with OutputStream

use of java.io.OutputStream in project enroscar by stanfy.

the class ImageRequest method writeBitmapToDisk.

void writeBitmapToDisk(final Bitmap bitmap) throws IOException {
    EnhancedResponseCache cache = (EnhancedResponseCache) manager.getImagesResponseCache();
    OutputStream output = new FileOutputStream(cache.getLocalPath(url));
    output = manager.getBuffersPool().bufferize(output, IMAGES_BUFFER_SIZE);
    try {
        final int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.PNG, quality, output);
    } finally {
        IoUtils.closeQuietly(output);
    }
}
Also used : EnhancedResponseCache(com.stanfy.enroscar.net.cache.EnhancedResponseCache) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream)

Aggregations

OutputStream (java.io.OutputStream)4178 IOException (java.io.IOException)1673 FileOutputStream (java.io.FileOutputStream)1331 InputStream (java.io.InputStream)1304 File (java.io.File)925 ByteArrayOutputStream (java.io.ByteArrayOutputStream)830 Test (org.junit.Test)735 BufferedOutputStream (java.io.BufferedOutputStream)477 FileInputStream (java.io.FileInputStream)431 Socket (java.net.Socket)375 ByteArrayInputStream (java.io.ByteArrayInputStream)233 URL (java.net.URL)231 OutputStreamWriter (java.io.OutputStreamWriter)230 HttpURLConnection (java.net.HttpURLConnection)189 BufferedInputStream (java.io.BufferedInputStream)178 InputStreamReader (java.io.InputStreamReader)176 BufferedReader (java.io.BufferedReader)159 FileNotFoundException (java.io.FileNotFoundException)158 GZIPOutputStream (java.util.zip.GZIPOutputStream)157 Path (java.nio.file.Path)155